package com.sjyl.kaoshitong.util;

import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

/**
 * Created by ChangerLow on 14-4-6.
 */
public class DBHelper extends SQLiteOpenHelper {

    private static final String DB_NAME ="kaoshitong";
    private static final int DB_VERSION = 1;
    private SQLiteDatabase db;

    public static  DBHelper mdbHelper;


    public static  DBHelper getInstance(Context context){
        if(mdbHelper == null){
            mdbHelper = new DBHelper(context);
        }
        return mdbHelper;
    }

    public DBHelper(Context context){
        super(context, DB_NAME, null, DB_VERSION);
    }

    public DBHelper(Context context, String name, SQLiteDatabase.CursorFactory factory, int version) {
        super(context, name, factory, version);
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        this.db = db;
       /* db.execSQL("CREATE TABLE [t_directory] (" +
                        "[_id] INTEGER PRIMARY KEY AUTOINCREMENT," +
                        "[data_id] TEXT UNIQUE NOT NULL,"+
                        "[data_version] INTEGER," +
                        "[data_content] TEXT,"+
                        "[down_code] TEXT," +
                        "[insert_time] TEXT," +
                        "[pid] TEXT,"+
                        "[down_status] TEXT,"+
                        "[down_url] TEXT,"+
                        "[data_type] INTEGER,"+
                        "[data_name] TEXT,"+
                        "[data_point] INTEGER," +
                        "[user_id] TEXT,"+
                        "[data_md5] TEXT,"+
                        "[data_src] TEXT"+
                        ")"
        );
        db.execSQL("CREATE TABLE [t_files] (" +
                        "[_id] INTEGER PRIMARY KEY AUTOINCREMENT," +
                        "[data_id] TEXT UNIQUE NOT NULL,"+
                        "[data_version] INTEGER," +
                        "[data_content] TEXT,"+
                        "[down_code] TEXT," +
                        "[insert_time] TEXT," +
                        "[pid] TEXT,"+
                        "[down_status] TEXT,"+
                        "[down_url] TEXT,"+
                        "[data_type] INTEGER,"+
                        "[data_name] TEXT,"+
                        "[data_point] INTEGER," +
                        "[user_id] TEXT,"+
                        "[data_md5] TEXT,"+
                        "[data_src] TEXT"+
                        ")"
        );*/
        /*db.execSQL("CREATE TABLE [t_download] (" +
                "[_id] TEXT NOT NULL," +
                "[user_id] TEXT NOT NULL,"+
                "[data_version] INTEGER," +
                "[pid] TEXT,"+
                "[down_status] TEXT,"+
                "[data_name] TEXT,"+
                "[data_src] TEXT,"+
                "PRIMARY KEY(_id, user_id))"
        );*/


    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int i, int i2) {
       // db.execSQL("drop table if exists [t_directory]");
       // db.execSQL("drop table if exists [t_files]");
        db.execSQL("drop table if exists [t_download]");
        /*
        db.execSQL("CREATE TABLE [t_directory] (" +
                        "[_id] INTEGER PRIMARY KEY AUTOINCREMENT," +
                        "[data_id] TEXT UNIQUE NOT NULL,"+
                        "[data_version] INTEGER," +
                        "[data_content] TEXT,"+
                        "[down_code] TEXT," +
                        "[insert_time] TEXT," +
                        "[pid] TEXT,"+
                        "[down_status] TEXT,"+
                        "[down_url] TEXT,"+
                        "[data_type] INTEGER,"+
                        "[data_name] TEXT,"+
                        "[data_point] INTEGER," +
                        "[user_id] TEXT,"+
                        "[data_md5] TEXT,"+
                        "[data_src] TEXT"+
                        ")"
        );
        db.execSQL("CREATE TABLE [t_files] (" +
                        "[_id] INTEGER PRIMARY KEY AUTOINCREMENT," +
                        "[data_id] TEXT UNIQUE NOT NULL,"+
                        "[data_version] INTEGER," +
                        "[data_content] TEXT,"+
                        "[down_code] TEXT," +
                        "[insert_time] TEXT," +
                        "[pid] TEXT,"+
                        "[down_status] TEXT,"+
                        "[down_url] TEXT,"+
                        "[data_type] INTEGER,"+
                        "[data_name] TEXT,"+
                        "[data_point] INTEGER," +
                        "[user_id] TEXT,"+
                        "[data_md5] TEXT,"+
                        "[data_src] TEXT"+
                        ")"
        );
        */
       /* db.execSQL("CREATE TABLE [t_download] (" +
                        "[_id] TEXT NOT NULL," +
                        "[user_id] TEXT NOT NULL,"+
                        "[data_version] INTEGER," +
                        "[pid] TEXT,"+
                        "[down_status] TEXT,"+
                        "[data_name] TEXT,"+
                        "[data_src] TEXT,"+
                        "PRIMARY KEY(_id, user_id))"
        );*/
    }

    public void execSQL(String sql, Object[] args)
    {
        SQLiteDatabase db = this.getWritableDatabase();
        db.execSQL(sql, args);
    }


    public Cursor query(String sql, String[] args)
    {
        SQLiteDatabase db = this.getReadableDatabase();
        Cursor cursor = db.rawQuery(sql, args);
        return cursor;
    }

}
